<template>
  <div class="add-address-page">
    <!-- 头部 -->
    <div class="header">
      <div class="back" @click="$router.back()">
        <i class="iconfont icon-back"></i>
      </div>
      <div class="title">新增送货地址</div>
    </div>

    <!-- 表单 -->
    <div class="form">
      <div class="form-item">
        <label>联系人：</label>
        <input type="text" v-model="form.contactName" placeholder="请输入联系人姓名">
      </div>
      <div class="form-item">
        <label>性别：</label>
        <div class="radio-group">
          <div class="radio" :class="{ active: form.contactSex === 1 }" @click="form.contactSex = 1">
            <span>先生</span>
          </div>
          <div class="radio" :class="{ active: form.contactSex === 0 }" @click="form.contactSex = 0">
            <span>女士</span>
          </div>
        </div>
      </div>
      <div class="form-item">
        <label>手机号：</label>
        <input type="tel" v-model="form.contactTel" placeholder="请输入手机号">
      </div>
      <div class="form-item">
        <label>收货地址：</label>
        <input type="text" v-model="form.address" placeholder="请输入详细地址">
      </div>
    </div>

    <!-- 保存按钮 -->
    <div class="save-btn" @click="saveAddress">保存</div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const userId = localStorage.getItem('userId')

// 表单数据
const form = ref({
  contactName: '',
  contactSex: 1,
  contactTel: '',
  address: ''
})

// 保存地址
const saveAddress = async () => {
  // 表单验证
  if (!form.value.contactName) {
    alert('请输入联系人姓名')
    return
  }
  if (!form.value.contactTel) {
    alert('请输入手机号')
    return
  }
  if (!form.value.address) {
    alert('请输入详细地址')
    return
  }

  try {
    await axios.post('http://localhost:8080/elm/deliveryAddress/saveDeliveryAddress', null, {
      params: {
        ...form.value,
        userId
      }
    })
    router.back()
  } catch (error) {
    console.error('保存地址失败:', error)
  }
}
</script>

<style scoped>
.add-address-page {
  padding-top: 44px;
  padding-bottom: 60px;
  background: #f5f5f5;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background: #0097ff;
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 15px;
  z-index: 100;
}

.back {
  padding: 10px;
  margin-left: -10px;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 16px;
}

.form {
  background: #fff;
  margin-top: 10px;
}

.form-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.form-item label {
  width: 80px;
  color: #333;
}

.form-item input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 14px;
}

.radio-group {
  flex: 1;
  display: flex;
}

.radio {
  padding: 5px 15px;
  border: 1px solid #ddd;
  border-radius: 20px;
  margin-right: 10px;
  font-size: 14px;
  color: #666;
}

.radio.active {
  background: #0097ff;
  color: #fff;
  border-color: #0097ff;
}

.save-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: #0097ff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}
</style>